﻿/* Code modified from http://coding4fun.codeplex.com/ */

using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

using System.Windows.Input;

namespace Outcoder.UI.Xaml.Controls
{
	public class InputPrompt : PopUp<string, PopUpResult>
	{
		protected Button OkButton;
		protected Button CancelButton;
		protected TextBox InputBox;
		const string OkButtonName = "okButton";
		const string CancelButtonName = "cancelButton";
		const string InputBoxName = "inputBox";

		public InputPrompt()
		{
			DefaultStyleKey = typeof(InputPrompt);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			if (OkButton != null)
			{
				OkButton.Click -= ok_Click;
			}

			if (CancelButton != null)
			{
				CancelButton.Click -= HandleCancelClick;
			}

			OkButton = GetTemplateChild(OkButtonName) as Button;
			CancelButton = GetTemplateChild(CancelButtonName) as Button;
			InputBox = GetTemplateChild(InputBoxName) as TextBox;

			if (InputBox != null)
			{
				// manually adding
				// GetBindingExpression doesn't seem to respect TemplateBinding
				// so TextBoxBinding's code doesn't fire

				var binding = new System.Windows.Data.Binding
				{
					Source = InputBox,
					Path = new PropertyPath("Text"),
				};
				
				SetBinding(ValueProperty, binding);
				//InputBox.Loaded += delegate
				//                    {
				//                        InputBox.Focus();
				//                        InputBox.SelectAll();
				//                    };
				//Focus();
				InputBox.Focus();
				InputBox.SelectAll();
				ThreadPool.QueueUserWorkItem(DelayInputSelect);
			}

			if (OkButton != null)
			{
				OkButton.Click += ok_Click;
				if (!CanCancel)
				{
					OkButton.Visibility = Visibility.Collapsed;
				}
			}

			if (CancelButton != null)
			{
				CancelButton.Click += HandleCancelClick;
			}

//			Loaded += delegate
//			          	{
//			          		DelayInputSelect(null);             		
//			          	};
		}

		void DelayInputSelect(object value)
		{
			return;
			Thread.Sleep(250);
			Dispatcher.BeginInvoke(() =>
			{
				if (InputBox == null)
				{
					return;
				}
				Focus();
				InputBox.SelectAll();
				InputBox.Focus();

//				Thread.Sleep(250);
//				ThreadPool.QueueUserWorkItem(delegate {
//					Thread.Sleep(250);
//				Dispatcher.BeginInvoke(() =>
//				{
//					Thread.Sleep(250);
//					InputBox.Focus();
//				});
//				});
			});
		}

		bool canCancel = true;

		public bool CanCancel
		{
			get
			{
				return canCancel;
			}
			set
			{
				canCancel = value;
			}
		}

		#region public InputScope InputScope
		/// <summary>
		/// Gets or sets the
		/// <see cref="T:System.Windows.Input.InputScope"/>
		/// used by the Text template part.
		/// </summary>
		public InputScope InputScope
		{
			get { return (InputScope)GetValue(InputScopeProperty); }
			set { SetValue(InputScopeProperty, value); }
		}

		/// <summary>
		/// Identifies the
		/// <see cref="P:Microsoft.Phone.Controls.AutoCompleteBox.InputScope"/>
		/// dependency property.
		/// </summary>
		public static readonly DependencyProperty InputScopeProperty =
			DependencyProperty.Register(
				"InputScope",
				typeof(InputScope),
				typeof(InputPrompt),
				null);
		#endregion public InputScope InputScope


		public string Value
		{
			get { return (string)GetValue(ValueProperty); }
			set { SetValue(ValueProperty, value); }
		}

		public static readonly DependencyProperty ValueProperty =
			DependencyProperty.Register("Value", typeof(string), typeof(InputPrompt), new PropertyMetadata(""));



		public string Title
		{
			get { return (string)GetValue(TitleProperty); }
			set { SetValue(TitleProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty TitleProperty =
			DependencyProperty.Register("Title", typeof(string), typeof(InputPrompt), new PropertyMetadata(""));

		public string Message
		{
			get { return (string)GetValue(MessageProperty); }
			set { SetValue(MessageProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty MessageProperty =
			DependencyProperty.Register("Message", typeof(string), typeof(InputPrompt), new PropertyMetadata(""));




		private void ok_Click(object sender, RoutedEventArgs e)
		{
			OnCompleted(new PopUpEventArgs<string, PopUpResult> { Result = Value, PopUpResult = PopUpResult.OK });
		}

		void HandleCancelClick(object sender, RoutedEventArgs e)
		{
			OnCompleted(new PopUpEventArgs<string, PopUpResult> { Result = Value, PopUpResult = PopUpResult.Cancelled });
		}
	}
}
